package com.szholly.pro.watergas_server.ps.android.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.util.RedisDbExecute;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.TwoTuple;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.JSONUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AndroidPipeDetail {
	/**
	 * 获取排水管网详情
	 * 
	 * @param parms
	 * @return
	 */
	public JSONObject getPipeDetail(HashMap<String, String> parms) {
		String sqlRowBase = " SELECT t.街道名称 || ' ' || t.本点号 show_name, t.行政区划 || t.街道名称 show_address,t.*"
				// + " t.本点号 as bdh,t.上点号 as sdh,t.管径或断面 as gjdm,t.路名 as
				// lm,t.产权单位 as cqdw,t.管养单位 as gydw,t.建成时间 as jcsj,"
				// + " t.设施状况 as sszk,t.备注 as bz,t.街道名称 as jdmc,t.行政区划 as
				// xzqh,t.排水 as ps,"
				+ " FROM SDE.ps_pipe t WHERE OBJECTID =@id ";
		List<GlobalParameter> pc = new ArrayList<>();
		pc.add(new GlobalParameter("id", parms.get("id")));

		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> attrs = DataUtil.getSelectRows(sqlRowBase, pc, pTable);
		Map name = new HashMap();
		if (null != attrs && attrs.size() > 0) {
			name = attrs.get(0);
		} else {
			return null;
		}

		name.remove("SHAPE");
		Map<String, String> data = new HashMap();
		Map<String, String> chidlobject = new HashMap();
		JSONObject mData = new JSONObject();
		chidlobject.put("项目编号", getString(name.get("PRJ_NO")));
		chidlobject.put("项目名称", getString(name.get("PRJ_NAME")));
		chidlobject.put("管线段唯一编号", getString(name.get("LNO")));
		chidlobject.put("起点管线点号", getString(name.get("S_PIONT")));
		chidlobject.put("起点管线埋深", getString(name.get("S_DEEP")));
		chidlobject.put("起点管内底标高", getString(name.get("IN_ELEV")));
		chidlobject.put("终点管线点号", getString(name.get("E_POINT")));
		chidlobject.put("终点管线埋深", getString(name.get("E_DEEP")));
		chidlobject.put("终点管内底标高", getString(name.get("OUT_ELEV")));
		chidlobject.put("所在污水系统", getString(name.get("SEWAGESYSTEM_ID")));
		chidlobject.put("所在雨水系统", getString(name.get("STORMSYSTEM_ID")));
		chidlobject.put("管线种类", getString(name.get("TYPE")));
		chidlobject.put("对象编码", getString(name.get("CODE")));
		chidlobject.put("压力类型", getString(name.get("PRESSURE_TYPE")));
		chidlobject.put("是否倒虹管", getString(name.get("INVERT_SILPHON")));
		chidlobject.put("材质", getString(name.get("MATERIAL")));
		chidlobject.put("材质使用寿命", getString(name.get("SERVICELIFE")));
		chidlobject.put("断面形式", getString(name.get("SHAPETYPE")));
		chidlobject.put("管径", getString(name.get("PSIZE")));
		chidlobject.put("管线长度", getString(name.get("PIPELENGTH")));
		chidlobject.put("流向", getString(name.get("FLOWDIR")));
		chidlobject.put("埋设方式", getString(name.get("EMBED")));
		chidlobject.put("接口方式", getString(name.get("INTERFACE")));
		chidlobject.put("是否预埋", getString(name.get("STYPE")));
		chidlobject.put("管线段地址", getString(name.get("ADDRESS")));
		chidlobject.put("管线段道路编码", getString(name.get("ROADCODE")));
		chidlobject.put("数据来源", getString(name.get("DATASOURCE")));
		chidlobject.put("探测单位", getString(name.get("SUNIT")));
		chidlobject.put("探测日期", getString(name.get("SDATE")));
		chidlobject.put("更新日期", getString(name.get("UPDATETIME")));
		chidlobject.put("埋设日期", getString(name.get("MDATE")));
		chidlobject.put("现状", getString(name.get("STATUS")));
		chidlobject.put("是否按计划", getString(name.get("PLAN")));
		chidlobject.put("所属工程名称", getString(name.get("ENAME")));
		chidlobject.put("设计单位", getString(name.get("DESIGN_DEPT")));
		chidlobject.put("施工单位", getString(name.get("CONSTER_DEPT")));
		chidlobject.put("权属单位", getString(name.get("BELONG")));
		chidlobject.put("运营单位", getString(name.get("OPERATOR")));
		chidlobject.put("线性", getString(name.get("LINE_TYPE")));
		chidlobject.put("备注", getString(name.get("NOTE")));
		chidlobject.put("省", getString(name.get("PROVINCE_DESC")));
		chidlobject.put("市", getString(name.get("CITY_DESC")));
		chidlobject.put("区", getString(name.get("DISTRICT_DESC")));
		chidlobject.put("街道", getString(name.get("STREET_DESC")));
		mData = JSONObject.fromObject(chidlobject);
		return mData;

	}

	/**
	 * 获取管网点详情
	 * 
	 * @param parms
	 * @return
	 */
	public String getGWDDetail(String station_code) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		JSONObject resultJson = new JSONObject();
		// 获取管网点基本信息
		String pipeSql = "select  name,'深圳市龙岗区金牛东路茜坑路段' as ADDRESS from BC_SITE_INFO t where t.exp_no='"
				+ station_code + "'";
		List<RowBase> pipePointData = DataUtil.getSelectRows(pipeSql, parms);
		JSONObject pipeJson = DataUtil.listToJSONObject(pipePointData);
		resultJson.put("pipePointData", pipeJson);

		// 获取实时数据
		String mnsql = "select t2.flow_type,t1.device_num from BC_COLLECTION_INFO t1 left join  monitor_station t2 on t1.site_id=t2.exp_no where t2.exp_no='"
				+ station_code + "'";
		List<RowBase> mnData = DataUtil.getSelectRows(mnsql, parms);
		RowBase r = mnData.get(0);
		String mn = r.getSafeString("DEVICE_NUM");
		HashMap<String, RowBase> readDataList = getStationRealTimeData(mn);
		List<RowBase> rList = new ArrayList<RowBase>();
		for(RowBase value:readDataList.values()){
			rList.add(value);
		}
		JSONArray jaList = JSONArray.fromObject(rList);
		JSONObject redata = new JSONObject();
		for(int i = 0 ;i<jaList.size();i++){
			if(!jaList.getJSONObject(i).optString("POLLNAME").equals("CODcr")){
				redata.put(jaList.getJSONObject(i).optString("POLLNAME")+"("+jaList.getJSONObject(i).optString("UNIT")+")", jaList.getJSONObject(i).optString("MEAS_NORMAL"));	
			}
			
		}
		resultJson.put("RealData", JSONObject.fromObject(readDataList).toString());
		return redata.toString();

	}

	/**
	 * 获取规划管网详情
	 * 
	 * @param parms
	 * @return
	 */
	public JSONObject getGHGWDetail(HashMap<String, String> parms) {
		String sql = "  SELECT t.jdname || ' ' || t.objectid show_name, t.ALLNAME show_address,t.* FROM sde.GHGW t WHERE OBJECTID =@id ";
		List<GlobalParameter> pc = new ArrayList<>();
		pc.add(new GlobalParameter("id", parms.get("id")));
		List<RowBase> attrs = DataUtil.getSelectRows(sql, pc);
		Map name = new HashMap();
		if (null != attrs && attrs.size() > 0) {
			name = attrs.get(0);
		} else {
			return null;
		}
		name.remove("SHAPE");
		Map<String, String> chidlobject = new HashMap();
		JSONObject mData = new JSONObject();
		chidlobject.put("街区地址", getString(name.get("SHOW_ADDRESS")));
		chidlobject.put("管网类型", getString(name.get("LAYER")));
		mData = JSONObject.fromObject(chidlobject);
		return mData;
	}

	/**
	 * 获取排水管网设施详情
	 * 
	 * @param parms
	 * @return
	 */
	public JSONObject getpipeDeviceDetail(HashMap<String, String> parms) {
		String sql = " SELECT t.街道名称 || ' ' || t.本点号 show_name, t.行政区划 || t.街道名称 show_address,t.* FROM sde.PS_POINT t WHERE OBJECTID =@id ";
		List<GlobalParameter> pc = new ArrayList<>();
		pc.add(new GlobalParameter("id", parms.get("id")));
		List<RowBase> attrs = DataUtil.getSelectRows(sql, pc);
		Map name = new HashMap();
		if (null != attrs && attrs.size() > 0) {
			name = attrs.get(0);
		} else {
			return null;
		}
		name.remove("SHAPE");
		Map<String, String> chidlobject = new HashMap();
		JSONObject mData = new JSONObject();
		chidlobject.put("名称", getString(name.get("附属物")));
		chidlobject.put("路名", getString(name.get("路名")));
		chidlobject.put("类型", getString(name.get("POINT_TYPE")));
		chidlobject.put("本点号", getString(name.get("本点号")));
		chidlobject.put("特征", getString(name.get("特征")));
		chidlobject.put("地面(m)", getString(name.get("地面")));
		chidlobject.put("埋深(m)", getString(name.get("埋深")));
		chidlobject.put("水深(m)", getString(name.get("水深")));
		chidlobject.put("淤泥", getString(name.get("淤泥")));
		chidlobject.put("产权单位", getString(name.get("产权单位")));
		chidlobject.put("营养单位", getString(name.get("营养单位")));
		chidlobject.put("建成时间", getString(name.get("建成时间")));
		mData = JSONObject.fromObject(chidlobject);
		return mData;
	}

	/**
	 * 获取清源小区详情
	 * 
	 * @param parms
	 * @return
	 */
	public JSONObject getqyxqDetail(HashMap<String, String> parms) {
		String sql = "  SELECT t.block || ' ' || t.jdxqid show_name, t.district || t.位置 show_address,t.* FROM sde.building t WHERE OBJECTID =@id ";
		List<GlobalParameter> pc = new ArrayList<>();
		pc.add(new GlobalParameter("id", parms.get("id")));

		List<RowBase> attrs = DataUtil.getSelectRows(sql, pc);

		Map name = new HashMap();
		if (null != attrs && attrs.size() > 0) {
			name = attrs.get(0);
		} else {
			return null;
		}
		name.remove("SHAPE");
		Map<String, String> chidlobject = new HashMap();
		JSONObject mData = new JSONObject();
		chidlobject.put("状态", getString(name.get("状态")));
		chidlobject.put("行政区划", getString(name.get("行政区划")));
		chidlobject.put("小区编号", getString(name.get("小区编号")));
		chidlobject.put("材质", getString(name.get("MATERIAL")));
		chidlobject.put("小区类型", getString(name.get("USEPROPERT")));
		chidlobject.put("面积", getString(name.get("BASEAREA")));
		chidlobject.put("年份", getString(name.get("NIAN")));
		chidlobject.put("墙体类型", getString(name.get("WALLTYPE")));
		mData = JSONObject.fromObject(chidlobject);
		return mData;
	}

	public String getString(Object value) {
		if (null != value) {
			return value.toString();
		} else {
			return " ";
		}
	}

	/**
	 * 获取站点实时数据
	 * 
	 * @param params
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public HashMap<String, RowBase> getStationRealTimeData(String mn) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateParse = new SimpleDateFormat("yyyyMMddHHmmss");
		String keyPrix = "stationInfo:" + mn + ":deviceInfo:";
		Set<String> keySet = RedisDbExecute.queryKeys(keyPrix + "*");

		String currentPoll = null;
		HashMap<String, RowBase> realTimeDataMap = new HashMap<>();
		RowBase row = null;

		for (String key : keySet) {
			String value = RedisDbExecute.getString(key);
			String oldValue = value.replace(".0", "");
			String simpleKeyName = key.replace(keyPrix, "");
			String poll = null;
			String pollName;
			String unit;// 单位
			String valueKey = null;// 传到前端的key值
			if (simpleKeyName.indexOf("_") > -1) {
				poll = simpleKeyName.substring(0, simpleKeyName.lastIndexOf("_"));
				pollName = translationPollDict(poll);
				valueKey = simpleKeyName.substring(simpleKeyName.lastIndexOf("_") + 1);
			} else {
				poll = simpleKeyName;
				pollName = translationPollDict(poll);
				valueKey = "_MEAS";
			}
			String timeStr = "";
			if (simpleKeyName.indexOf("DataTime") > -1) {
				try {
					timeStr = dateFormat.format(dateParse.parse(value));
					realTimeDataMap.put(poll, row);
					row.setValue("DATATIME", timeStr);
				} catch (Exception e) {

				}
			}
			// 没有记录的参数不翻译不处理
			if (pollName == null || pollName.equals("")) {
				continue;
			}

			if (simpleKeyName.indexOf("_MEAS") > -1) {
				try {
					BigDecimal doubleValue = new BigDecimal(value);
					value = doubleValue.setScale(3, BigDecimal.ROUND_HALF_EVEN).doubleValue() + "";
				} catch (Exception e) {

				}
			}

			unit = getPollUnit(poll);

			if (!poll.equals(currentPoll)) {
				row = new RowBase();
				realTimeDataMap.put(poll, row);
				currentPoll = poll;
			}

			Double alarmUp = RedisHelper.getInstance().getPollAlarmUp(mn, poll);
			Double alarmDown = RedisHelper.getInstance().getPollAlarmDown(mn, poll);
			Double vd = RedisDbExecute.getDouble(key + "_NORMAL");
			String value_status = "zc";
			if (vd != null && alarmDown != null && alarmUp != null && key.indexOf("B01") < 0
					&& key.indexOf("B01TOTAL") < 0) {
				if (vd > 0) {
					if (vd > alarmUp) {
						value = ">" + alarmUp;
						value_status = "cb";
					} else if (vd < alarmDown) {
						value = "<" + alarmDown;
						value_status = "yc";
					} else {

					}
				} else {
					value = "异常";
					value_status = "yc";
				}
			}
			row.setValue(valueKey + "_NORMAL", RedisDbExecute.getString(key + "_NORMAL"));
			row.setValue("UNIT", unit);
			row.setValue(valueKey, value);
			row.setValue(valueKey + "_status", value_status);
			row.setValue(valueKey + "_old", oldValue);
			row.setValue("pollName", pollName);
			row.setValue("alarmUp", alarmUp);
			row.setValue("alarmDown", alarmDown);
			row.setValue("poll", poll);
			row.setValue("UNIT", unit);
		}

		return realTimeDataMap;
	}

	/**
	 * 翻译污染物 gislin 2016年4月22日
	 * 
	 * @return
	 */
	private static String translationPollDict(String code) {
		String text = RedisHelper.getInstance().translationDictValue("MONITOR_PARAM", code);
		return text;
	}

	/**
	 * 获取污染物单位 gislin 2016年4月22日
	 * 
	 * @return
	 */
	private static String getPollUnit(String code) {
		String text = RedisHelper.getInstance().translationDictUnit("MONITOR_PARAM", code);
		return text;
	}
}
